
DECLARE
V_COUNT       NUMBER;
V_SQL VARCHAR2(20000);

BEGIN
DBMS_OUTPUT.ENABLE(buffer_size => null);
<#if tableList??&&(tableList?size>0)>
    <#list tableList as t>
        BEGIN
        SELECT COUNT(1) INTO V_COUNT FROM USER_TABLES T WHERE T.TABLE_NAME='${t.name}';
        IF V_COUNT <= 0 THEN
        V_SQL := 'CREATE TABLE ${t.name}'||
        '('||
        <#if t.tableColumns??&&(t.tableColumns?size>0)>
            <#list t.tableColumns as column>
                <#if column_index!=0>
                    ','||
                </#if>
                <#if column.columnName??&&column.columnName!="">
                    ' ${column.columnName} '||
                </#if>
                ' ${column.columnOracleType()} '||
                <#if column.columnLength??&&column.columnLength!="">
                    '(${column.columnLength}'||
                </#if>
                <#if column.columnPrecision??&&column.columnPrecision!="">
                    ',${column.columnPrecision})'||
                <#elseif column.columnLength??&&column.columnLength!="">
                    ') '||
                </#if>
                <#if column.columnDefault??&&column.columnDefault!="">
                    ' default ${column.columnDefault} '||
                </#if>
                <#if column.getNullable()>
                    ' NOT NULL' ||
                </#if>
            </#list>
        </#if>
        ')'||
        <#if t.tableSpaces??&&t.tableSpaces!="">
            'tablespace ${t.tableSpaces}'
        </#if>;
        EXECUTE IMMEDIATE V_SQL;
        <#if t.tableAnnotation??&&t.tableAnnotation!="">
            EXECUTE IMMEDIATE 'COMMENT ON TABLE ${t.name} IS ''${t.tableAnnotation}''';
        </#if>
        <#list t.tableColumns as column>
            <#if column.columnAnnotation??&&column.columnAnnotation!="">
                EXECUTE IMMEDIATE 'COMMENT ON COLUMN ${t.name}.${column.columnName} IS ''${column.columnAnnotation}''';
            </#if>
        </#list>
        DBMS_OUTPUT.PUT_LINE('${t.name} 表新建成功');
        <#if t.tablePrimaryKeys?? && (t.tablePrimaryKeys?size > 0) >
            <#list t.tablePrimaryKeys as primaryKey>
                <#if primaryKey.keyType=="PRIMARY_KEY">
                    V_SQL := 'ALTER TABLE ${t.name} ADD CONSTRAINT ${primaryKey.primaryKeyName} primary key (${primaryKey.primaryKeyColumn}) using index'||
                    <#if primaryKey.tablespace??&&primaryKey.tablespace!="">
                        ' tablespace ${primaryKey.tablespace} '
                    </#if>;
                    EXECUTE IMMEDIATE V_SQL;
                    DBMS_OUTPUT.PUT_LINE('${primaryKey.primaryKeyName} 主键新建成功');
                </#if>
                <#if primaryKey.keyType=="UNIQUE">
                    V_SQL := 'ALTER TABLE ${t.name} ADD CONSTRAINT ${primaryKey.primaryKeyName} unique (${primaryKey.primaryKeyColumn}) using index'||
                    <#if primaryKey.tablespace??&&primaryKey.tablespace!="">
                        ' tablespace ${primaryKey.tablespace} '
                    </#if>;
                    EXECUTE IMMEDIATE V_SQL;
                    DBMS_OUTPUT.PUT_LINE('${primaryKey.primaryKeyName} 唯一键新建成功');
                </#if>

            </#list>

        </#if>
        ELSE
        DBMS_OUTPUT.PUT_LINE('${t.name} 表已存在');
        END IF;
        exception when  others then
        dbms_output.put_line('错误号：'||sqlcode);
        dbms_output.put_line('错误信息：'||sqlerrm);
        END;
    </#list>
</#if>
END;
